package com.seguriapp.crimeactivity.gwt.client.activity;

import java.util.List;

import net.customware.gwt.dispatch.client.DispatchAsync;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.seguriapp.common.gwt.client.AbstractSeguriAppActivity;
import com.seguriapp.common.gwt.client.AbstractSeguriAppPlace;
import com.seguriapp.common.gwt.client.SeguriAppCallback;
import com.seguriapp.common.gwt.client.SeguriAppClientSession;
import com.seguriapp.common.gwt.client.SeguriAppDisplay;
import com.seguriapp.crimeactivity.gwt.shared.GetCrimeActivityDataAction;
import com.seguriapp.crimeactivity.gwt.shared.GetCrimeActivityDataResult;
import com.seguriapp.crimeactivity.gwt.shared.GetCrimeCategoryListAction;
import com.seguriapp.crimeactivity.gwt.shared.GetCrimeCategoryListResult;
import com.seguriapp.entities.Complaint;
import com.seguriapp.entities.CrimeCategory;

public class CrimeActivityActivity extends AbstractSeguriAppActivity {

  private final Display display;
  private int zoomValue = 13;
  private boolean getUserLocation;

  public CrimeActivityActivity(final DispatchAsync dispatchAsync, final EventBus eventBus, final PlaceController placeController, final Display display, final AbstractSeguriAppPlace place, final SeguriAppClientSession clientSession) {
    super(dispatchAsync, eventBus, placeController, place, clientSession);
    display.setPresenter(this);
    this.display = display;
  }

  @Override
  protected void onStart(final AcceptsOneWidget widget, final EventBus eventBus) {
    widget.setWidget(display.asWidget());
    getUserLocation = true;
    getCategoryListFromServer();
  }

  private void getCategoryListFromServer() {
    dispatchAsync.execute(new GetCrimeCategoryListAction(), new SeguriAppCallback<GetCrimeCategoryListResult>() {

      public void onSuccess(final GetCrimeCategoryListResult result) {
        display.setCrimeCategoryList(result.getCrimeCategoryList());
        getInitialCrimeActivityData(result.getCrimeCategoryList());
      }

    });
  }

  private void getInitialCrimeActivityData(final List<CrimeCategory> crimeCategoryList) {
    String crimeCategoryListStr = "";
    for (final CrimeCategory crimeCategory : crimeCategoryList) {
      crimeCategoryListStr = crimeCategoryListStr.concat(crimeCategory.getId().toString()).concat(",");
    }
    if (!crimeCategoryListStr.isEmpty()){
      crimeCategoryListStr = crimeCategoryListStr.substring(0, crimeCategoryListStr.length() - 1);
    }
    final GetCrimeActivityDataAction action = new GetCrimeActivityDataAction();
    action.setOptions(crimeCategoryListStr);
    getCrimeActivityData(action);
  }

  private void getCrimeActivityData(final GetCrimeActivityDataAction action) {
    action.setZoomLevel(zoomValue);
    dispatchAsync.execute(action, new SeguriAppCallback<GetCrimeActivityDataResult>() {

      public void onSuccess(final GetCrimeActivityDataResult result) {
        display.setMapData(result.getCriminalItemsList(), result.getDefaultLatitude(), result.getDefaultLongitude(), zoomValue, action.getOptions(), getUserLocation);
        getUserLocation = false;
      }

      public void onFailure(final Throwable e) {
        Window.alert("Ocurrio un error al obtener la data requerida");
      }
    });
  }

  public void updateMapData(final String options) {
    final GetCrimeActivityDataAction action = new GetCrimeActivityDataAction();
    action.setOptions(options);
    getCrimeActivityData(action);
  }

  public void setZoomValue(final int zoomLevel) {
    zoomValue = zoomLevel;
  }

  public interface Display extends SeguriAppDisplay {

    /**
     * setea el presenter del activity
     */
    void setPresenter(CrimeActivityActivity presenter);

    void setCrimeCategoryList(List<CrimeCategory> crimeCategoryList);

    void setMapData(List<Complaint> list, double defaultLat, double defaultLng, int zooomValue, String mapOptions, boolean getUserLocation);
  }

}
